﻿using CollegeReviewApp.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CollegeReviewApp.DataModels;
using CollegeReviewApp.Data;

namespace CollegeReviewApp.Web.IDataAdapters.Adapters
{
    public class CollegeReviewAdapter : ICollegeReviewAdapter
    {
        /****************COLLEGE CRUD*********/
        public List<College> GetAllColleges()
        {
            List<College> colleges;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                colleges = db.Colleges.Include("ReviewList").OrderBy(x => x.Name).ToList();
            }
            return colleges;
        }

        public College GetCollege(int id)
        {
            College college;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Find(id);
            }
            return college;
        }
        public int AddCollege(College college)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Add(college);
                db.SaveChanges();
            }
            return college.Id;
        }

        public College UpdateCollege(College college)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College UpdatingCollege = db.Colleges.Find(college.Id);
                UpdatingCollege.setProps(college);
                db.SaveChanges();
            }
            return college;
        }

        public void DeleteCollege(int id)
        {
            bool runner = false;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.Find(id);
                db.Colleges.Remove(college);
                db.SaveChanges();
                runner = true;
            }
            while (!runner) { };
        }
        /**********END COLLEGE CRUD**************/
        /**************Reviews CRUD**************/
        public List<Review> GetAllReviews(int id)
        {
            List<Review> reviews;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                reviews = db.Reviews.Where(x=>x.CollegeId==id).ToList();
            }
            return reviews;
        }

        public Review AddReview(Review review)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                review = db.Reviews.Add(review);
                db.SaveChanges();
            }
            return review;
        }

        public Review UpdateReview(Review review)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Review UpdatingReview = db.Reviews.Find(review.Id);
                UpdatingReview.SetProps(review);
                db.SaveChanges();
            }
            return review;
        }

        public void DeleteReview(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Review review = db.Reviews.Find(id);
                db.Reviews.Remove(review);
                db.SaveChanges();
            }
        }
        /***********END Reviews CRUD************/



    }
}